package deploy;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import server.DataNode;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

import static common.Utils.remoteIp;

public class DNDeploy {
    private final String IP;
    private final int PORT;
    private final int T_PORT;
    private final String storagePath;
    private static final Logger logger = LoggerFactory.getLogger(DataNode.class);


    public DNDeploy(String IP, int PORT, int T_PORT, String storagePath){
        this.IP = IP;
        this.PORT = PORT;
        this.T_PORT = T_PORT;
        this.storagePath = storagePath;
    }
    void server() throws RemoteException {
        try {
            System.setProperty("java.rmi.server.hostname", remoteIp);
            LocateRegistry.createRegistry(PORT);
            DataNode dn = new DataNode(IP,PORT,T_PORT,storagePath);
            String location = "//localhost"+":"+PORT+"/DN"+IP+PORT;
            Naming.rebind(location,dn);
            logger.info("DataNode start listen in {}",location);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}
